package com.caimore.module.mc8332;

import com.caimore.common.Serial_For_Smslib;
import com.caimore.common.SmsUtils_sdk;
import com.caimore.entity.SMSMessageMgr;
import com.caimore.entity.SendSmsParam;
import com.caimore.modulefactory.IsmsWriter;
import java.util.ArrayList;

public class ATWriterThreadPDU8332 implements IsmsWriter {

   Serial_For_Smslib SerilaSmslib;
   private volatile boolean shutdown = false;
   private volatile boolean sendMs = false;
   Thread t1 = null;


   public ATWriterThreadPDU8332(Serial_For_Smslib _smslib_test, boolean _shutdown) {
      this.SerilaSmslib = _smslib_test;
      this.shutdown = _shutdown;
   }

   public void start() {
      this.t1 = new Thread() {
         public void run() {
            String e;
            String lstSendContext;
            char[] i;
            try {
               e = "AT+CPIN ? \r";
               char[] sReceivedNum = e.toCharArray();
               ATWriterThreadPDU8332.this.SerilaSmslib.writeByte(sReceivedNum);
               System.out.println("[ATWriterThreadPDU8332]sSend-AT-Command:" + e);

               while(!SMSMessageMgr.getInstance().getREADY() && !SMSMessageMgr.getInstance().getREADY()) {
                  ;
               }

               if(SMSMessageMgr.getInstance().getREADY()) {
                  for(int oSendSmsParam = 0; oSendSmsParam < 2; ++oSendSmsParam) {
                     if(oSendSmsParam == 0) {
                        lstSendContext = "AT+CNMI=2,1,1,1,0\r";
                        i = lstSendContext.toCharArray();
                        ATWriterThreadPDU8332.this.SerilaSmslib.writeByte(i);
                        System.out.println("[ATWriterThreadPDU8332]sSend-AT-Command: " + lstSendContext);
                     } else if(oSendSmsParam == 1) {
                        lstSendContext = "AT+CMGF=1\r";
                        i = lstSendContext.toCharArray();
                        ATWriterThreadPDU8332.this.SerilaSmslib.writeByte(i);
                        System.out.println("[ATWriterThreadPDU8332]sSend-AT-Command: " + lstSendContext);
                     }
                  }
               }
            } catch (Exception var15) {
               System.out.println(var15.getMessage());
            }

            while(!ATWriterThreadPDU8332.this.shutdown) {
               try {
                  if(SMSMessageMgr.getInstance().getREADY()) {
                     e = null;
                     String var16 = null;
                     SendSmsParam var17 = SMSMessageMgr.getInstance().delSendSmsParam();
                     if(var17 != null) {
                        e = var17.getContent();
                        var16 = var17.getPhone();
                     }

                     if(e == null && var16 == null && !ATWriterThreadPDU8332.this.sendMs) {
                        lstSendContext = "AT+CMGL=0\r";
                        i = lstSendContext.toCharArray();
                        ATWriterThreadPDU8332.this.SerilaSmslib.writeByte(i);
                        System.out.println("[ATWriterThreadPDU8332]sSend-AT-Command: " + lstSendContext);
                        var17 = SMSMessageMgr.getInstance().delSendSmsParam();
                        if(var17 != null) {
                           e = var17.getContent();
                           var16 = var17.getPhone();
                        }
                     } else {
                        ATWriterThreadPDU8332.this.sendMs = true;
                     }

                     ArrayList var18 = new ArrayList();
                     if(e != null) {
                        if(e.length() <= 35) {
                           var18.add(e);
                        } else {
                           while(e.length() > 35) {
                              String var19 = e.substring(0, 35);
                              var18.add(var19);
                              e = e.substring(35);
                              if(e.length() <= 35) {
                                 var18.add(e);
                              }
                           }
                        }

                        int var20 = 0;

                        while(var20 < var18.size()) {
                           String sTempBody = (String)var18.get(var20);

                           for(int j = 0; j < 3; ++j) {
                              System.out.println("SmsUtils.getInstance().getOK(): " + SMSMessageMgr.getInstance().getOK());
                              System.out.println("SmsUtils.getInstance().getCR(): " + SMSMessageMgr.getInstance().getCR());
                              String sCMGS;
                              char[] byCMGS;
                              if(j == 0) {
                                 sCMGS = "AT+CMGF=1\r";
                                 byCMGS = sCMGS.toCharArray();
                                 ATWriterThreadPDU8332.this.SerilaSmslib.writeByte(byCMGS);
                                 System.out.println("[ATWriterThreadPDU8332]sSend-AT-Command: " + sCMGS);
                                 Thread.sleep(2000L);
                              } else if(j == 1) {
                                 sCMGS = "AT+ZMSGL=6,4\r";
                                 byCMGS = sCMGS.toCharArray();
                                 ATWriterThreadPDU8332.this.SerilaSmslib.writeByte(byCMGS);
                                 System.out.println("[ATWriterThreadPDU8332]sSend-AT-Command: " + sCMGS);
                                 Thread.sleep(2000L);
                              } else if(j == 2) {
                                 sCMGS = "AT+CMGS=\"" + var16 + "\"\r";
                                 byte[] var21 = sCMGS.getBytes();
                                 byte[] byTempBody = SmsUtils_sdk.encode8332UCS2(sTempBody);
                                 byte[] chSendContextByte = new byte[var21.length + byTempBody.length];
                                 System.arraycopy(var21, 0, chSendContextByte, 0, var21.length);
                                 System.arraycopy(byTempBody, 0, chSendContextByte, var21.length, byTempBody.length);
                                 if(chSendContextByte != null) {
                                    byte[] newOrder = new byte[chSendContextByte.length + 2];
                                    System.arraycopy(chSendContextByte, 0, newOrder, 0, chSendContextByte.length);
                                    newOrder[chSendContextByte.length] = 0;
                                    newOrder[chSendContextByte.length + 1] = 26;
                                    ATWriterThreadPDU8332.this.SerilaSmslib.writeByte8332(newOrder);
                                    System.out.println("[ATWriterThreadPDU8332]sSend-ContextBody:" + new String(newOrder));
                                    System.out.println("\r\n\r\n");
                                 }

                                 ++var20;
                                 Thread.sleep(2000L);
                              }
                           }

                           Thread.sleep(2000L);
                        }

                        ATWriterThreadPDU8332.this.sendMs = false;
                     }
                  }

                  Thread.sleep(1000L);
               } catch (InterruptedException var13) {
                  Thread.currentThread().interrupt();
               } catch (Exception var14) {
                  System.out.println(var14.getMessage());
               }
            }

         }
      };
      this.t1.start();
   }

   public final void shutdownRequest() {
      this.shutdown = true;
      this.t1.interrupt();
   }
}
